package com.xqt.service.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.xqt.base.dao.BaseDao;
import com.xqt.base.entity.BaseEntity;
import com.xqt.base.entity.Operator;
import com.xqt.base.service.BaseService;
import com.xqt.constant.Globals;
import com.xqt.core.enums.CommonEnum;
import com.xqt.dao.sys.SysMemberDao;
import com.xqt.dao.sys.SysRoleDao;
import com.xqt.entity.sys.SysMember;
import com.xqt.entity.sys.SysRoleMember;
import com.xqt.inters.service.sys.ISysMemberService;
import com.xqt.util.PasswordUtil;
import com.xqt.util.SessionLocalUtils;
import com.xqt.util.StringUtil;

/**
 * Created by Administrator on 2017/3/16.
 */
public class SysMemberService extends BaseService<SysMember> implements ISysMemberService{

    @Autowired
    private SysMemberDao sysMemberDao;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Override
    public void add(SysMember sysMember) throws Exception {
         super.add(sysMember);
    }

    public void update(SysMember sysMember) throws Exception {
         preUpdate(sysMember);
         sysMemberDao.updateBySelective(sysMember);
    }

    @Override
    public void updateBySelective(SysMember sysMember) throws Exception {
        preUpdate(sysMember);
        sysMemberDao.updateBySelective(sysMember);
    }

    @Override
    public void delete(Object... ids) {
        if (ids == null || ids.length < 1) {
            return;
        }
        // 删除缓存中对应所属关系
        for (Object id : ids) {
            sysRoleDao.deleteRoleMemberByMemberId(id);
            sysMemberDao.delete(id);
        }
    }


    @Override
    public int queryByCount(BaseEntity page) {
        return sysMemberDao.queryByCount(page);
    }

    @Override
    public List<SysMember> queryByList(BaseEntity page) {
        return sysMemberDao.queryByList(page);
    }

    /*特有方法*/
    @Override
    public SysMember queryByName(Object name){
        return sysMemberDao.queryByName(name);
    }

    @Override
    public SysMember queryByMobile(Object mobile) {
        return sysMemberDao.queryByMobile(mobile);
    }

    @Override
    public List<SysMember> selectByRoleId(Object roleId) {
        return sysMemberDao.selectByRoleId(roleId);
    }

    @Override
    public SysMember queryByItemId(SysMember sysMember) {
        return sysMemberDao.queryByItemId(sysMember);
    }

    @Override
    public void addOrUpdate(SysMember member, String checkedRole,Operator operator) throws Exception {
        // 新增人员保存
        if (StringUtil.isEmpty(member.getId())) {

            // 密码验证
            String password = PasswordUtil.encrypt(member.getMemName(),member.getMemPwd(), PasswordUtil.getStaticSalt());
            member.setMemPwd(password);
            member.setMemParentId(operator.getMember().getId());
            add(member);
        } else {
            // 更新人员保存
            updateBySelective(member);
            member = queryById(member.getId());
        }

        // 删除原先分配的记录
        sysRoleDao.deleteRoleMemberByMemberId(member.getId());

        // 保存选择的菜单
        if (!StringUtil.isEmpty(checkedRole)) {

            String[] roleList = checkedRole.split(Globals.COMMA);
            SysRoleMember roleMember = null;
            for (String roleId : roleList) {
                roleMember = new SysRoleMember();
                roleMember.setRoleId(roleId);
                roleMember.setMemberId(member.getId());
                preInsert(roleMember);
                sysRoleDao.addRoleMember(roleMember);
            }
        }
    }

    @Override
    public void regist(SysMember sysMember) throws Exception {
        // 对密码进行二次加密保存
        String password = PasswordUtil.encrypt(sysMember.getMemName(),
                sysMember.getMemPwd(), PasswordUtil.getStaticSalt());
        // 设置用户初始状态未不可用
        sysMember.setMemStatus(CommonEnum.mem_status.STATUS_2.getValue());
        sysMember.setMemPwd(password);
        // 保存用户数据
        add(sysMember);
    }

	@Override
	public BaseDao<SysMember> getDao() {
		return sysMemberDao;
	}
}
